package com.sunwoda.demo.service.impl;

import com.sunwoda.demo.entity.User;
import com.sunwoda.demo.dao.UserDao;
import com.sunwoda.demo.service.UserService;
import com.sunwoda.demo.utils.SaltUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2021-03-17 15:13:03
 */
@Service("userService")
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Long id) {
        return this.userDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.userDao.deleteById(id) > 0;
    }

    @Override
    public String login(String userName, String password) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            log.error("用户名不存在");
        }catch (IncorrectCredentialsException e){
            log.error("无效密码");
        }

        if (subject.isAuthenticated()){
            return "redirect:/index.jsp";
        }else {
            return "redirect:/login.jsp";
        }
    }

    @Override
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }

    @Override
    public String registered(String userName, String password) {
        String salt = SaltUtil.getSalt(4);
        Md5Hash md5Hash = new Md5Hash(password,salt,1024);
        Date date = new Date();
        User user = new User();
        user.setUsername(userName);
        user.setPassword(md5Hash.toHex());
        user.setCreateAt(date);
        user.setUpdateAt(date);
        user.setSalt(salt);
        try {
            userDao.insert(user);
        }catch (Exception e){
            e.printStackTrace();
            log.error("注册失败");
            return "registered";
        }
        return "login";
    }
}
